//
//  LyricLabel.m
//  GcwSDKDemo
//
//  Created by 陈超 on 2017/7/25.
//  Copyright © 2017年 陈超. All rights reserved.
//

#import "LyricLabel.h"
#import "UIUtils.h"
@implementation LyricLabel
{
    NSTimer *timer;
    NSUInteger count;
    BOOL blockPlay;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)awakeFromNib{
    [super awakeFromNib];
       count=0;
    
}
-(void)setLyricUrl:(NSURL *)lyrcUrl{
    NSData *data=[NSData dataWithContentsOfURL:lyrcUrl];
    self.lyricItems=[LyricUtils convertLyricUrlToArray:data];
}
-(void)startPlay{
    
    if(timer==nil){
            timer=[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:(@selector(updateLyric)) userInfo:nil repeats:YES];
        count=0;
    }
    blockPlay=NO;

}
-(void)updateLyric{
    if(blockPlay) return;
    count++;
   
    [self.lyricItems enumerateObjectsUsingBlock:^(LyricItem *item, NSUInteger idx, BOOL * _Nonnull stop) {
        float seconds=count*0.2;
        if(seconds>item.startTime&&seconds<(item.duration+item.startTime)){
            self.text=item.content;
            if(item.lyricImage==nil){
                item.lyricImage=[UIUtils viewShot:self];//将歌词转换为图片
            }
            item.leftPercent=0;//歌词label在视频中显示的位置百分比
            item.topPercent=0.9;
            item.heightPercent=0.10;//歌词label的高占显示容器的高度的百分比
            item.widthPercent=1.0f;
            *stop=YES;
            
        }
        if(idx==self.lyricItems.count-1&&seconds>(item.duration+item.startTime)){//歌词已经播放完
            blockPlay=YES;
        }
        
    }];
 
}
-(void)pause{
    blockPlay=YES;
}
-(void)stop{
    blockPlay=YES;
    self.text=@"";
    [timer invalidate];
}
@end
